package com.koicarp.university.graduate.service.service.information.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koicarp.university.graduate.service.dao.CollectDao;
import com.koicarp.university.graduate.service.dto.wx.CollectInfoDto;
import com.koicarp.university.graduate.service.entity.Collect;
import com.koicarp.university.graduate.service.service.information.ICollectService;
import com.koicarp.university.graduate.service.vo.wx.CollectInfoVo;
import com.koicarp.university.graudate.common.constant.BaseConstant;
import com.koicarp.university.graudate.common.utils.UniversityResponse;
import com.koicarp.university.graudate.common.utils.UniversityUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 【毕业生管理：资讯收藏表】(Collect)表服务实现类
 *
 * @author liutao
 * @since 2020-12-07 10:22:42
 */
@Service("collectService")
@RequiredArgsConstructor
public class CollectServiceImpl extends ServiceImpl<CollectDao,Collect> implements ICollectService {

    private final CollectDao collectDao;

    @Override
    public UniversityResponse getCollectInfoWithPage(CollectInfoVo vo) {
        UniversityResponse response = new UniversityResponse();
        Page<Collect> page = null;
        page = new Page<>(vo.getPageNum(),vo.getPageSize(),true);
        IPage<CollectInfoDto> result = null;
        result = this.collectDao.queryCollectInfoWithPage(page, vo);
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        Map<String, Object> dataTable = UniversityUtil.getDataTable(result);
        response.data(dataTable);
        return response;
    }

    @Override
    public UniversityResponse getCollectInfoByGraAndInfo(Collect collect) {
        UniversityResponse response = new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        List<Collect> list = collectDao.queryAll(collect);
        if(list.size()==1){
            return response;
        }else if (list.size() > 1){
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message("数据库数据错误！！");
        }else{
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message("没找到该数据");
        }
        return response;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Collect queryById(Integer id) {
        return this.collectDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Collect> queryAllByLimit(int offset, int limit) {
        return this.collectDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param collect 实例对象
     * @return 实例对象
     */
    @Override
    public UniversityResponse insert(Collect collect) {
        UniversityResponse response = new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        int i = this.collectDao.insert(collect);
        if (i<0){
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }

    /**
     * 修改数据
     *
     * @param collect 实例对象
     * @return 实例对象
     */
    @Override
    public Collect update(Collect collect) {
        this.collectDao.update(collect);
        return this.queryById(collect.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @return 是否成功
     */
    @Override
    public UniversityResponse deleteByGraIdWithInfoId(Collect collect) {
        UniversityResponse response = new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);

        int i = collectDao.deleteByGraIdWithInfoId(collect);
        if(i< 0){
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }
}